<?php
namespace App\Http\Controllers\Member;

use App\Models\UserStudy;
use Illuminate\Http\Request;
use App\Models\Teaches;
use App\Models\TeachesChapter;
use App\Models\TeachesContent;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
/**
 *
 * @author Joker
 *        
 */
class StudyController extends MemberController
{
    
    public function index()
    {
        $sort = Input::get('sort',0);
        $study = new UserStudy();
        $result = $study->getList($this->uid,$sort);
        
        return view('member.study.course')->with('studys',$result)->with('sort',$sort);
    }
    
    /**
     * 用户将课程加入学习
     */
    public function store(Request $req)
    {
        $data = $req->only('app','appid','chapterid','contentid');
        $teaches = Teaches::find($data['appid']);
        if(empty($teaches))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            $chapters = $teaches->chapters;
            $chapters_count = count($chapters);
            if($chapters <= 0)
            {
                return  $this->formatJson('falied','抱歉，该资源不存在相关学习内容!');
            }
            else 
            {   
                $chapterid =TeachesChapter::where(array('teaches_id'=>$data['appid'],'id'=>$data['chapterid']))->first();
                if(empty($data['chapterid']) || empty($chapterid))
                {
                    $data['chapterid'] = TeachesChapter::where('teaches_id','=',$data['appid'])->first()->id;
                
                }
                $contentid = TeachesContent::where(array('chapter_id'=>$data['chapterid'],'id'=>$data['contentid']))->first();
                if(empty($data['contentid']) || empty($contentid))
                {
                    $data['contentid'] = TeachesContent::where('chapter_id','=',$data['chapterid'])->first()->id;
                }
                $chapter_num = 0;
                foreach ($chapters as $k=>$chp)
                {
                    if($chp->id == $data['chapteid'])
                        $chapter_num = $k+1;
                }
                $hasStudy = UserStudy::where(array('app_id'=>$data['appid'],'app'=>$data['app'],'user_id'=>$this->uid))->first();
                if(empty($hasStudy))
                {
                    DB::transaction(function() use ($data,$chapters_count,$teaches,$chapter_num){
                        $insert_rows = array(
                            'app'       =>  $data['app'],
                            'app_id'    =>  $data['app_id'],
                            'chapter_id'=>  $data['chapter_id'],
                            'content_id'=>  $data['content_id'],
                            'chapters'  =>  $data['chapters'],
                            'price'     =>  $data['price'],
                            'chapter_num'=> $data['chapter_num']
                        );
                        $study = UserStudy::firstOrCreate($insert_rows);
                        if($study->price > 0)
                        {
                            //积分计算
                        }
                    });
                }
                else
                {
                    if($chapter_num > $hasStudy->chapter_num)
                    {
                        $hasStudy->chapter_num = $chapter_num;
                    }
                    if($data['chapterid'] > $hasStudy->chapter_id)
                    {
                        $hasStudy->chapter_id = $data['chapterid'];
                    }
                    if($data['contentid'] > $hasStudy->content_id)
                    {
                        $hasStudy->content_id = $data['contentid'];
                    }
                    $hasStudy->chapters = $chapters_count;
                    $hasStudy->save();
                }
            }
        }
    }
    
    /**
     * 取消已学习课程
     * @param integer $id
     */
    public function destroy($id)
    {
        $study = UserStudy::where(array('id'=>$id,'user_id'=>$this->uid))->first();
        if(empty($study))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            $study->delete();
            return $this->formatJson('success','取消学习成功！');
        }
    }
}